﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.Web.WebControls;
using System.Web.UI.WebControls;
using EPiServer.DataAbstraction;

namespace Dropit.BlogPageProvider.WebControls
{
	public class InputPageType : InputBase
	{
		private DropDownList _dropDownList;

		protected override void CreateChildControls()
		{
			_dropDownList = new DropDownList();
			base.CopyWebAttributes(_dropDownList);
			_dropDownList.Items.Add(new ListItem(string.Empty, "-1"));
			var listItems = from pageType in PageType.List()
											orderby pageType.LocalizedName descending
											select new ListItem(pageType.LocalizedName, pageType.ID.ToString());
			_dropDownList.Items.AddRange(listItems.ToArray());
			_dropDownList.Attributes.Add("OnChange", "if(window.On" + this.ClientID + "Change) window.On" + this.ClientID + "Change(this)");
			this.Controls.Add(_dropDownList);
		}

		public int SelectedPageTypeId
		{
			get
			{
				this.EnsureChildControls();
				int id;
				int.TryParse(_dropDownList.SelectedValue, out id);
				return id;
			}
			set
			{
				this.EnsureChildControls();
				_dropDownList.SelectedValue = value.ToString();
			}
		}
	}
}